package net.ipinglun.module.newsList;

import android.content.Context;
import android.util.Log;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.orhanobut.hawk.Hawk;
import com.orhanobut.logger.Logger;

import net.ipinglun.util.BasePresenter;
import net.ipinglun.util.HttpHelper;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class NewsPresenter extends BasePresenter {

    private int mChannelId;
    private static final String TAG = "NewsList";


    public void setChannelId(int channelId) {
        mChannelId = channelId;
    }


    public void getNewsList(int pager, final GetNewsListResponseHandler handler) {

        final String cacheKey = "news_list_" + mChannelId + "_" + pager;

        NewsListBean cacheList = Hawk.get(cacheKey, new NewsListBean());

        String url;
        if (mChannelId == 0) {
            url = "http://api.wallstreetcn.com/v2/posts?type=news&page=" + pager;
        } else {
            url = "http://api.wallstreetcn.com/v2/posts?cid=11&page=" + pager;
        }

        handler.onStart();

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {




                        NewsListBean list = new NewsListBean();

                        try {
                            String resStr = response.getString("results");

                            Logger.d("hello");
                            Logger.e("hello");
                            Logger.w("hello");
                            Logger.v("hello");
                            Logger.wtf("hello");
                            Logger.json(resStr);
                            //Logger.xml(XML_CONTENT);

                            JSONArray jsonArray = new JSONArray(resStr);


                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject obj = jsonArray.getJSONObject(i);
                                NewsBean news = new NewsBean();
                                news.id = obj.getString("id");
                                news.title = obj.getString("title");
                                news.image_url = obj.getString("imageUrl");

                                list.newsList.add(news);
                            }

                            // 加入缓存
                            Hawk.put(cacheKey, list);

                        } catch (JSONException e) {

                        }

                        handler.onSuccess(list.newsList);
                        handler.onFinish();
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                        handler.onFailure();
                        handler.onFinish();
                    }
                }
        );


        jsonObjectRequest.setRetryPolicy(new
                DefaultRetryPolicy(HttpHelper.TIME_OUT,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

        HttpHelper.getInstance().addToRequestQueue(jsonObjectRequest, TAG);


    }
}
